<?php
/* @var $this PurchaseOrderController */
/* @var $model PurchaseOrder */

$this->breadcrumbs=array(
	'Purchase Orders'=>array('index'),
	$model->id,
);

$this->menu=array(

	array('label'=>'Add Purchase Order Details', 'url'=>array('/purchaseOrderDetails/create', 'id'=>$model->id)),
    array('label'=>'Create Delivery Receipt', 'url'=>array('/deliveryReceipt/create', 'id'=>$model->id)),
	array('label'=>'List PurchaseOrder', 'url'=>array('index')),
	array('label'=>'Create PurchaseOrder', 'url'=>array('create')),
	array('label'=>'Update PurchaseOrder', 'url'=>array('update', 'id'=>$model->id)),
	array('label'=>'Delete PurchaseOrder', 'url'=>'#', 'linkOptions'=>array('submit'=>array('delete','id'=>$model->id),'confirm'=>'Are you sure you want to delete this item?')),
	array('label'=>'Manage PurchaseOrder', 'url'=>array('admin')),
	
);
?>

<h1>Purchase Order #<?php echo $model->id; ?></h1>

<?php $this->widget('zii.widgets.CDetailView', array(
	'data'=>$model,
	'attributes'=>array(
		array('label'=>'Supplier', 'value'=>$model->supplier->supplier_name),
		array('label'=>'Project', 'value'=>$model->project->project_name),),)); ?>
<br><br>
<h2>Purchase Order Items</h2>
<?php 
$config = array();
$dataProvider = new CArrayDataProvider($rawData=$model->purchaseOrderDetails, $config);

$this->widget('zii.widgets.grid.CGridView', array('dataProvider'=>$dataProvider, 'columns'=>array(
        array('name'=>'item_code', 'header'=> 'Item Code', 'value'=>'$data->item->item_code'),
		array('name'=>'item_id', 'header'=> 'Items', 'value'=>'$data->item->item_desc'),
		array('name' => 'Quantity', 'value' => '$data->qty' .' ." " .$data->item->unit ' ),
		array('name' => 'Amount', 'value' => ' "Php" ." " .$data->amount'),
		array(
            'class'=>'CButtonColumn'
            , 'viewButtonUrl'=>'Yii::app()->createUrl("/PurchaseOrderDetails/view", array("id"=>$data["id"]))'
            , 'updateButtonUrl'=>'Yii::app()->createUrl("/PurchaseOrderDetails/update", array("id"=>$data["id"]))'
            , 'deleteButtonUrl'=>'Yii::app()->createUrl("/PurchaseOrderDetails/delete", array("id"=>$data["id"]))'
            )
    )
));



if(isset($_GET['purchaseOrderDetailsid'])){
		$purchaseOrderDetailsid = $_GET['purchaseOrderDetailsid']; 
		
		$sql = "Select * from purchase_order_details where id = '$purchaseOrderDetailsid'";
$dataReader =  Yii::app()->db->createCommand($sql)->query();
while(($row = $dataReader->read())!==false) {$pull = $row;}
 $item_id=$pull['item_id'];

$sql2 = "Select unit_price from item where id = '$item_id'";
$dataReader =  Yii::app()->db->createCommand($sql2)->query();
while(($row = $dataReader->read())!==false) {$pull2 = $row;}

$request_amount = ($pull['qty'] * $pull2['unit_price']);	
$sql1 = "update purchase_order_details set amount='$request_amount' where id='$purchaseOrderDetailsid'";
$dataReader =  Yii::app()->db->createCommand($sql1)->query();

 $this->redirect(array('/purchaseOrder/view','id'=> $model->id));
		
		}else{
    	echo '';
		}

		
$total = Yii::app()->db->createCommand("SELECT SUM(`amount`) AS `amount` FROM `purchase_order_details` where `purchase_order_id` = '$model->id'")->queryAll();





if(isset($total[0]['amount'])){
echo '<div id="yw3" class="grid-view">';
 echo '<table class="items">';
      echo '<th>Total Amount</th><td class ="odd">'.' Php ' .($total[0]['amount']).'</td></tr>'; 
	  echo '</table></div>';
		}else{
    	echo '';}
?>


<br> 
	
<?php	
$config = array();
$dataProvider = new CArrayDataProvider($rawData=$model->deliveryReceipts, $config);

$this->widget('zii.widgets.grid.CGridView', array('dataProvider'=>$dataProvider, 'columns'=>array(
  
		array( 'header'=> 'Delivery Receipt(s) ID', 'value'=>'$data->id'),

		array(
            'class'=>'CButtonColumn'
            , 'viewButtonUrl'=>'Yii::app()->createUrl("/DeliveryReceipt/view", array("id"=>$data["id"]))'
			 , 'updateButtonUrl'=>'Yii::app()->createUrl("/DeliveryReceipt/update", array("id"=>$data["id"]))'
            , 'deleteButtonUrl'=>'Yii::app()->createUrl("/DeliveryReceipt/delete", array("id"=>$data["id"]))'
           
            )
    )
));
?>

<?php $en=purchaseOrder::model()->findAll('id = :a', array(':a'=>$model->id));?>
<?php if (count($en) !== 0){?>
<br>
</center>
<h2>Project</h2>
<?php foreach ($en as $row) { ?>
<?php echo CHtml::link ( 'Update', array('project/update', 'id'=>$row->project_id)); ?>
	<?php $this->widget ('zii.widgets.CdetailView', array('data'=>$row,'attributes'=>array(
    array('label'=>'Project Name' , 'value'=>$row->project->project_name),
	array('label'=>'Point Person' , 'value'=>$row->project->point_person),
	array('label'=>'Start Date' , 'value'=>$row->project->start_date),
	array('label'=>'End Date' , 'value'=>$row->project->end_date),
	array('label'=>'Total Cost' ,  'value' => 'Php ' .$model->project->total_cost),
	array('label'=>'Total Expenses' ,  'value' => 'Php ' .$model->project->total_expenses),
	array('label'=>'User', 'value'=>$model->project->user->fname .' '. $model->project->user->lname),
	),));?><br>
	
<?php }} ?>


<?php $en=purchaseOrder::model()->findAll('id = :a', array(':a'=>$model->id));?>
<?php if (count($en) !== 0){?>
<br>
<h2>Supplier</h2>
<?php foreach ($en as $row) { ?>
<?php echo CHtml::link ( 'Update', array('supplier/update', 'id'=>$row->supplier_id)); ?>
	<?php $this->widget ('zii.widgets.CdetailView', array('data'=>$row,'attributes'=>array(
    array('label'=>'Supplied By' , 'value'=>$row->supplier->supplier_name),
	array('label'=>'Contact Person' , 'value'=>$row->supplier->contact_person),
	array('label'=>'Mobile Number' , 'value'=>$row->supplier->mobile_no),
	array('label'=>'Phone Number' , 'value'=>$row->supplier->phone_no),
	array('label'=>'Fax' , 'value'=>$row->supplier->fax),
	array('label'=>'Email' , 'value'=>$row->supplier->email),),));?><br>
<?php }} ?>







